﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_BUSINESS;
using QLTH_DTO;
using unitlControl;

namespace QuanLy_THPT_S4.HocSinh
{
    public partial class frmHosSinhUPDATE : Form
    {
        private readonly clsDanTocBU _dtbu;
        private readonly clsTonGiaoBU _tgbu;
        private readonly clsPhanLoaiBU _plbu;
        private readonly clsHocSinhBU _hsbu;
        private readonly clsHocSinhDTO _hsdto;

        private String _mahs;

        public String Mahs
        {
            get { return _mahs; }
            set { _mahs = value; }
        }



        public frmHosSinhUPDATE()
        {
            InitializeComponent();
            _dtbu = new clsDanTocBU();
            _tgbu = new clsTonGiaoBU();
            _plbu = new clsPhanLoaiBU();
            _hsbu = new clsHocSinhBU();
            _hsdto = new clsHocSinhDTO();

        }



        private void btnThoatHS_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmHosSinhUPDATE_Load(object sender, EventArgs e)
        {
            rdoPhai.SelectedIndex = 0;
            ComboUtils.SetDataSuorce(cboTonGiao, _tgbu.DanhSachTonGiao(), "MATG", "TENTONGIAO",0);
            ComboUtils.SetDataSuorce(cboPhanLoai, _plbu.DSPhanLoai(), "MAPL", "TENPL", 0);
            ComboUtils.SetDataSuorce(cboDanToc, _dtbu.DanhSachDanToc(), "MADT", "TENDANTOC", 0);
            if (Mahs == "" || Mahs == null)
            {
                return;
            }
            else
            {
                txtMaHS.Text = Mahs;
                txtHoTen.Text = _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["HOTEN"].ToString();
                dtNgaySinh.EditValue = _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["NGAYSINH"];
                txtNoiSinh.Text = _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["NOISINH"].ToString();
                txtDiaChi.Text = _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["DIACHI"].ToString();
                dtNgayNhapHoc.EditValue = _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["NGAYNHAPHOC"];
                ComboUtils.SelectedItem(cboDanToc, _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["DANTOC"].ToString());
                ComboUtils.SelectedItem(cboPhanLoai, _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["PHANLOAI"].ToString());
                ComboUtils.SelectedItem(cboTonGiao, _hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["TONGIAO"].ToString());
                if (_hsbu.Lay_HS_TheoMa(Mahs).Rows[0]["PHAI"].ToString() == "Nam")
                {
                    rdoPhai.SelectedIndex = 0;
                }
                else
                {
                    rdoPhai.SelectedIndex = 1;
                }


            }


        }

        private void btnLuuHS_Click(object sender, EventArgs e)
        {
            _hsdto.Holoths = toancuc.Ho_Ten(txtHoTen.Text)[0].Trim();
            _hsdto.Tenhs = toancuc.Ho_Ten(txtHoTen.Text)[1].Trim();
            _hsdto.Phai = int.Parse(rdoPhai.EditValue.ToString());
            _hsdto.Ngaysinh = DateTime.Parse(dtNgaySinh.EditValue.ToString());
            _hsdto.Ngaynhaphoc = DateTime.Parse(dtNgayNhapHoc.EditValue.ToString());
            _hsdto.Diachi = txtDiaChi.Text;
            _hsdto.Noisinh = txtNoiSinh.Text;
            _hsdto.Tongiao.Matg = ComboUtils.LayGT_Combobox(cboTonGiao);
            _hsdto.Dantoc.Madt = ComboUtils.LayGT_Combobox(cboDanToc);
            _hsdto.Phanloai.Mapl = ComboUtils.LayGT_Combobox(cboPhanLoai);

            if (txtMaHS.Text == "" || txtMaHS.Text == null)
            {
                _hsdto.Mahs = toancuc.Tao_ma(_hsbu.macuoi(), "HS", 5);
                _hsbu.ThemHS(_hsdto);
                MessageBox.Show("Bạn đã thêm thành công");
                btnNhapLaiHS_Click(sender, e);

            }
            else
            {
                _hsdto.Mahs = txtMaHS.Text;
                _hsbu.CapNhatHS(_hsdto);

            }

        }

        private void btnNhapLaiHS_Click(object sender, EventArgs e)
        {
            txtHoTen.Text = "";
            txtHoTen.Focus();
            txtDiaChi.Text = "";
            txtNoiSinh.Text = "";
            ComboUtils.SetDataSuorce(cboDanToc, _dtbu.DanhSachDanToc(), "MADT", "TENDANTOC",0);
            ComboUtils.SetDataSuorce(cboTonGiao, _tgbu.DanhSachTonGiao(), "MATG", "TENTONGIAO",0);
            ComboUtils.SetDataSuorce(cboPhanLoai, _plbu.DSPhanLoai(), "MAPL", "TENPL", 0);

        }

  
    }
}
